package com.xiaouyudeguang.common.resource;

import com.fasterxml.jackson.databind.JsonNode;
import com.xiaouyudeguang.common.collect.MapBuilder;
import com.xiaouyudeguang.common.constants.Headers;
import com.xiaouyudeguang.common.dicode.service.DiCodeService;
import com.xiaouyudeguang.common.dirule.entity.DiRule;
import com.xiaouyudeguang.common.dirule.service.DiRuleService;
import com.xiaouyudeguang.common.utils.DroolsUtils;
import com.xiaouyudeguang.common.exception.BusinessException;
import com.xiaouyudeguang.common.request.RequestDto;
import com.xiaouyudeguang.common.respose.ResultDto;
import com.xiaouyudeguang.common.utils.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.kie.api.runtime.KieSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Slf4j
@RestController
public class DroolsRuleResource {

    @Autowired
    private DiCodeService diCodeService;

    @Autowired
    private DiRuleService diRuleService;

    @RequestMapping(value = "/v{version}/{table_name}/{method_name}", method = {RequestMethod.POST, RequestMethod.PUT})
    public ResultDto<Object> resource(@RequestHeader(Headers.TENANT_ID) String tenantId,
                                      @RequestHeader(Headers.APP_ID) String appId,
                                      @PathVariable("version") String version,
                                      @PathVariable("table_name") String tableName,
                                      @PathVariable("method_name") String methodName,
                                      @RequestBody RequestDto<JsonNode> requestDto) throws Exception {
        ResultDto<Object> resultDto = new ResultDto<>();
        Map<String, String> props = MapBuilder.of();
        props.put("tenantId", tenantId);
        props.put("appId", appId);
        props.put("tableName", tableName);
        props.put("methodName", methodName);
        props.put("userId", SecurityUtils.getUserId());
        DiRule diRule = diRuleService.getRule(tenantId, appId, version, tableName,  methodName);
        if (diRule == null) {
            throw new BusinessException("无访问权限");
        }
        KieSession kieSession = DroolsUtils.newKieSession(diRule.getContent(), diRule.getName(), props);
        kieSession.insert(requestDto);
        kieSession.setGlobal("log", log);
        kieSession.setGlobal("service", diCodeService);
        kieSession.insert(resultDto);
        kieSession.fireAllRules();
        return resultDto;
    }

    @RequestMapping(value = "/v{version}/{table_name}/{method_name}/{id}", method = {RequestMethod.GET, RequestMethod.DELETE})
    public ResultDto<Object> resource(@RequestHeader(Headers.TENANT_ID) String tenantId,
                                      @RequestHeader(Headers.APP_ID) String appId,
                                      @PathVariable("version") String version,
                                      @PathVariable("table_name") String tableName,
                                      @PathVariable("method_name") String methodName,
                                      @PathVariable("id") String id) throws Exception {
        ResultDto<Object> resultDto = new ResultDto<>();
        Map<String, String> props = MapBuilder.of();
        props.put("tenantId", tenantId);
        props.put("appId", appId);
        props.put("tableName", tableName);
        props.put("methodName", methodName);
        props.put("id", id);
        props.put("userId", SecurityUtils.getUserId());
        DiRule diRule = diRuleService.getRule(tenantId, appId, version, tableName,  methodName);
        if (diRule == null) {
            throw new BusinessException("无访问权限");
        }
        KieSession kieSession = DroolsUtils.newKieSession(diRule.getContent(), diRule.getName(), props);
        kieSession.setGlobal("log", log);
        kieSession.setGlobal("service", diCodeService);
        kieSession.insert(resultDto);
        kieSession.fireAllRules();
        return resultDto;
    }
}
